package com.gdut.huayan.controller;

import com.gdut.huayan.entity.dto.Result;
import com.gdut.huayan.entity.po.LimitMoney;
import com.gdut.huayan.entity.vo.StockSaveVo;
import com.gdut.huayan.service.StockService;
import com.gdut.huayan.validation.group.GroupAdd;
import com.gdut.huayan.validation.group.GroupUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.groups.Default;

@RestController
@RequestMapping("/stock")
public class StockController {

    @Autowired
    private StockService stockService;

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PostMapping("/limit-money/add")
    public Result addLimitMoney(@RequestBody @Validated({Default.class, GroupAdd.class})LimitMoney limitMoney) {
        return Result.success(stockService.addLimitMoney(limitMoney));
    }

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @GetMapping("/limit-money/delete")
    public Result deleteLimitMoney(Long id) {
        stockService.deleteLimitMoney(id);
        return Result.success();
    }

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PostMapping("/limit-money/update")
    public Result updateLimitMoney(@RequestBody @Validated({Default.class, GroupUpdate.class}) LimitMoney limitMoney) {
        stockService.updateLimitMoney(limitMoney);
        return Result.success();
    }

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @GetMapping("/limit-money/get")
    public Result getAllLimitMoney() {
        return Result.success(stockService.getAllLimitMoney());
    }

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PostMapping("/add")
    public Result addStock(@RequestBody @Validated({Default.class, GroupAdd.class})StockSaveVo stockSaveVo) {
        return Result.success(stockService.addStock(stockSaveVo));
    }

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @DeleteMapping("/delete")
    public Result deleteStock(Long id) {
        stockService.deleteStock(id);
        return Result.success();
    }

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PostMapping("/update")
    public Result updateStock(@RequestBody @Validated({Default.class, GroupUpdate.class}) StockSaveVo stockSaveVo) {
        return Result.success(stockService.updateStock(stockSaveVo));
    }

}
